<?php
	
	require_once("nusoap/nusoap.php");
	class LeadSoap{
    	var $proxy;
        var $sess;
		var $moduleName;
		var $defaultOrderBy;
		var $columnsList;
		var $rowCount;
		
        function LeadSoap(){
		
			$this->proxy = $_SESSION["sugar_soap_proxy"];
			$this->sess = $_SESSION["sugar_session_id"];
			$this->rowCount = 0;
			$this->moduleName = "Leads";
			$this->defaultOrderBy = " leads.date_modified desc";
			$this->columnsList = array();
			$this->columnsList = array(
            		'id',
					'lead_source',
					'campaign_id',
					'lead_source_description',
					'refered_by',
					'salutation',
					'first_name',
					'last_name',
					'account_name',
					'title',
					'department',
					'do_not_call',
					'assigned_user_id',
					'status',
					'status_description',
					'phone_work',
					'phone_mobile',
					'phone_home',
					'phone_other',
					'phone_fax',
					'email1',
					'primary_address_street',
					'primary_address_city',
					'primary_address_state',
					'primary_address_postalcode',
					'primary_address_country',
					'alt_address_street',
					'alt_address_city',
					'alt_address_state',
					'alt_address_postalcode',
					'alt_address_country',
					'description',
					'latitude_c',
					'longitude_c',
					'direction_c',
					'date_modified',
					'created_by',
					'deleted'
        		);
			
        }
				
				
		function getLeads($query, $startIndex, $numberOfRows, $orderBy, $includeDeleted) {
			
			if ($orderBy == "")
				$orderBy = $this->defaultOrderBy;
		
    		$result = $this->proxy->get_entry_list(
        		$this->sess,
        		$this->moduleName,
        		$query,
        		$orderBy,
        		$startIndex,
        		$this->columnsList,
        		$numberOfRows,
        		false
    		);
			
			$returnList = array();
			
			if ($result == "" || count($result) == 0)
				return $returnList;
			
			foreach ($result['entry_list'] as $record) {
				$returnList[] = $record;
			}
			
			$this->rowCount = count($returnList);
			
			if ($includeDeleted && $this->rowCount < $numberOfRows) {
				
			
				if ($deleteNumberOfRows != -1) {
					$deleteResult = $this->getDeletedLeads($query, 0,  $numberOfRows - $this->rowCount, $orderBy);
					
					if ($deleteResult != "" && count($deleteResult) > 0) {
					
						foreach ($deleteResult['entry_list'] as $record) {
							$returnList[] = $record;
						}
						
						$this->rowCount = count($returnList);
					}
					
				}
			} 
			
    		return $returnList;
		}
		
		function getRowCount() {
		
			return $this->rowCount;
		}
		
		
		function getTotalRows($query, $includeDeleted) {
		
			$result = $this->proxy->get_entries_count(
        		$this->sess,
        		$this->moduleName,
        		$query,
				$includeDeleted);
				
			return $result['result_count'];
	
		}
		
		function getDeletedLeads($query, $startIndex, $numberOfRows, $orderBy=' leads.date_modified desc'){
    		$result = $this->proxy->get_entry_list(
        		$this->sess,
        		$this->moduleName,
        		$query,
        		$orderBy,
        		$startIndex,
        		$thos->columnsList,
        		$numberOfRows,
        		true
    		);
			
    		return $result;
		}
		
		
		function saveLead($id, $lead_source, $campaign_id, $lead_source_description, $refered_by,
							$salutation, $first_name, $last_name, $account_name, $title,
							$department, $do_not_call, $assigned_user_id, $status, $status_description,
							$phone_work, $phone_mobile, $phone_home, $phone_other, $phone_fax, $email1, 
							$primary_address_street, $primary_address_city, $primary_address_state, 
							$primary_address_postalcode, $primary_address_country, $alt_address_street, 
							$alt_address_city, $alt_address_state, $alt_address_postalcode, $alt_address_country, 
							$description, $latitude_c, $longitude_c, $direction_c, $date_modified) {
		
		
			if ($id) {
				$response = $this->proxy->set_entry($this->sess, 'Leads',
								array(array("name" => "id", "value" => $id),
									array("name" => "lead_source", "value"  => $lead_source),
									array("name" => "campaign_id", "value"  => $campaign_id),
									array("name" => "lead_source_description", "value"  => $lead_source_description),
									array("name" => "refered_by", "value"  => $refered_by),
									array("name" => "salutation", "value"  => $salutation),
									array("name" => "first_name", "value"  => $first_name),
									array("name" => "last_name", "value"  => $last_name),
									array("name" => "account_name", "value"  => $account_name),
									array("name" => "title", "value"  => $title),
									array("name" => "department", "value"  => $department),
									array("name" => "do_not_call", "value"  => $do_not_call),
									array("name" => "assigned_user_id", "value"  => $assigned_user_id),
									array("name" => "status", "value"  => $status),
									array("name" => "status_description", "value"  => $status_description),
									array("name" => "phone_work", "value"  => $phone_work),
									array("name" => "phone_mobile", "value"  => $phone_mobile),
									array("name" => "phone_home", "value"  => $phone_home),
									array("name" => "phone_other", "value"  => $phone_other),
									array("name" => "phone_fax", "value"  => $phone_fax),
									array("name" => "email1", "value"  => $email1),
									array("name" => "primary_address_street", "value"  => $primary_address_street),
									array("name" => "primary_address_city", "value"  => $primary_address_city),
									array("name" => "primary_address_state", "value"  => $primary_address_state),
									array("name" => "primary_address_postalcode", "value"  => $primary_address_postalcode),
									array("name" => "primary_address_country", "value"  => $primary_address_country),
									array("name" => "alt_address_street", "value"  => $alt_address_street),
									array("name" => "alt_address_city", "value"  => $alt_address_city),
									array("name" => "alt_address_state", "value"  => $alt_address_state),
									array("name" => "alt_address_postalcode", "value"  => $alt_address_postalcode),
									array("name" => "alt_address_country", "value"  => $alt_address_country),
									array("name" => "description", "value"  => $description),
									array("name" => "latitude_c", "value"  => $latitude_c),
									array("name" => "longitude_c", "value"  => $longitude_c),
									array("name" => "direction_c", "value" => $direction_c)
								)
							);
			} else {
				$response = $this->proxy->set_entry($this->sess, 'Leads',
								array(array("lead_source" => $lead_source),
									array("name" => "lead_source", "value"  => $lead_source),
									array("name" => "campaign_id", "value"  => $campaign_id),
									array("name" => "lead_source_description", "value"  => $lead_source_description),
									array("name" => "refered_by", "value"  => $refered_by),
									array("name" => "salutation", "value"  => $salutation),
									array("name" => "first_name", "value"  => $first_name),
									array("name" => "last_name", "value"  => $last_name),
									array("name" => "account_name", "value"  => $account_name),
									array("name" => "title", "value"  => $title),
									array("name" => "department", "value"  => $department),
									array("name" => "do_not_call", "value"  => $do_not_call),
									array("name" => "assigned_user_id", "value"  => $assigned_user_id),
									array("name" => "status", "value"  => $status),
									array("name" => "status_description", "value"  => $status_description),
									array("name" => "phone_work", "value"  => $phone_work),
									array("name" => "phone_mobile", "value"  => $phone_mobile),
									array("name" => "phone_home", "value"  => $phone_home),
									array("name" => "phone_other", "value"  => $phone_other),
									array("name" => "phone_fax", "value"  => $phone_fax),
									array("name" => "email1", "value"  => $email1),
									array("name" => "primary_address_street", "value"  => $primary_address_street),
									array("name" => "primary_address_city", "value"  => $primary_address_city),
									array("name" => "primary_address_state", "value"  => $primary_address_state),
									array("name" => "primary_address_postalcode", "value"  => $primary_address_postalcode),
									array("name" => "primary_address_country", "value"  => $primary_address_country),
									array("name" => "alt_address_street", "value"  => $alt_address_street),
									array("name" => "alt_address_city", "value"  => $alt_address_city),
									array("name" => "alt_address_state", "value"  => $alt_address_state),
									array("name" => "alt_address_postalcode", "value"  => $alt_address_postalcode),
									array("name" => "alt_address_country", "value"  => $alt_address_country),
									array("name" => "description", "value"  => $description),
									array("name" => "latitude_c", "value"  => $latitude_c),
									array("name" => "longitude_c", "value"  => $longitude_c),
									array("name" => "direction_c", "value" => $direction_c)
								)
							);
            }			
					 
			return $response;
		}
		
		function deleteLead($id) {
		
			$response = $this->proxy->set_entry($this->sess, 'Leads', 
								array(array("name" => 'id',"value" => $id),
									  array("name" => 'deleted',"value" => 1),
            						 )
            				);
		
			return $response;
		
		}
		
		function nameValuePairToSimpleArray($array){
    		$my_array=array();
    		while(list($name,$value)=each($array)){
        		$my_array[$value['name']]=$value['value'];
    		}
    		return $my_array;
		} 
		
		
	}
	
$leadSoap = new LeadSoap();

					
$leadsArray = array("" => "",
				    "Cold Call" => "Cold Call",
					"Existing Customer" => "Existing Customer",
					"Self Generated" => "Self Generated",
					"Employee" => "Employee",
					"Partner" => "Partner",
					"Public Relations" => "Public Relations",
					"Direct Mail" => "Direct Mail",
					"Conference" => "Conference",
					"Trade Show" => "Trade Show",
					"Web Site" => "Web Site",
					"Word of mouth" => "Word of mouth",
					"Email" => "Email",
					"Campaign" => "Campaign",
					"Other" => "Other");
					
$statusArray = array ("" => "",
					"New" => "New",
					"Assigned" => "Assigned",
					"In Process" => "In Process",
					"Converted" => "Converted",
					"Recycled" => "Recycled",
					"Dead" => "Dead");
					
$salutationsArray = array("" => "", 
 						  "Mr." => "Mr.", 
						  "Ms." => "Ms.", 
						  "Mrs." => "Mrs.", 
						  "Dr." => "Dr.", 
						  "Prof." => "Prof.");

?>